from j264.tools.attributes.Attribute import Attribute


class FloatAttribute(Attribute):

    ### INITIALIZER ###

    def __init__(self, section=None, minimum=None, maximum=None):
        Attribute.__init__(self, section)
        assert isinstance(minimum, (int, float, type(None)))
        assert isinstance(maximum, (int, float, type(None)))
        if minimum is not None:
            minimum = float(minimum)
        self._minimum = minimum
        if maximum is not None:
            maximum = float(maximum)
        self._maximum = maximum

    ### READ-ONLY PUBLIC ATTRIBUTES ###

    @property
    def maximum(self):
        return self._maximum

    @property
    def minimum(self):
        return self._minimum

    ### PUBLIC METHODS ###

    def get_input_massage(self):
        def massage(self, arg):
            return float(arg)
        return massage

    def get_input_predicate(self):
        minimum = self.minimum
        maximum = self.maximum
        def predicate(self, arg):
            if not isinstance(arg, (int, float)):
                return False
            arg  = float(arg)
            if minimum is not None and arg < minimum:
                return False
            if maximum is not None and maximum < arg:
                return False
            return True
        return predicate            
